<cfcomponent output="false">
	
	<!--- --->

	<cffunction name="saveImportant" access="public" output="true" returntype="string">
		<cfargument name="id" required="false" default="" />
		<cfargument name="name" required="true" />
		<cfargument name="sort_order" required="true" />
		<cfargument name="complete" required="false" default="0" />
		<cfargument name="completed_on" required="true" default="" />
		<cfargument name="userID" required="false" default="#request.session.user.id#" />
		
		<cfset var local = {} />
		
		<cfset local.id = $.database.save(
			table="important",
			id=arguments.id,
			name=arguments.name,
			user_id=arguments.userID,
			sort_order=arguments.sort_order,
			expiration_date=$.run.it('importantService.getExpirationDate'),
			complete=arguments.complete,
			completed_on=arguments.completed_on,
			week=100 <!--- legacy value no longer used --->
		) />

		<cfreturn local.id />
	
	</cffunction>

	<!--- --->
	
	<cffunction name="saveComplete" access="public" output="true" returntype="string">
		<cfargument name="id" required="false" default="" />
		<cfargument name="complete" required="false" default="true">
		
		<cfset var local = {} />
		
		<cfif arguments.complete>
			<cfset arguments.complete = 1 />
		<cfelse>
			<cfset arguments.complete = 0 />
		</cfif>
				
		<cfset local.id = $.database.save(
			table="important",
			id=arguments.id,
			complete=arguments.complete,
			completed_on=dateTimeFormat(now(),'yyyy-mm-dd HH:nn:ss')
		) />

		<cfreturn true />
	
	</cffunction>

	<!--- --->
	
	<cffunction name="remove" access="public" output="true" returntype="string">
		<cfargument name="id" required="true" />
		
		<cfset $.database.delete(
			table="important",
			id=arguments.id
		) />

		<cfreturn arguments.id />
	
	</cffunction>

	<!--- --->
	
	<cffunction name="getImportant" access="public" output="true" returntype="query">
		<cfargument name="lastweek" required="false" default="false" />
		
		<cfset var local = {} />
		
		<!--- keep this old logic --->
		<cfset local.date = $.date.applyLocalization(now()) />

		<cfset local.currentWeek = week(local.date) />
		
		<cfset arguments.week = week(local.date) />
		
		<cfif arguments.lastweek and local.currentWeek eq 1>
			<cfset arguments.week = 53 />
			<cfset arguments.week2 = 52 />
		<cfelseif arguments.lastweek>
			<cfset arguments.week = local.currentWeek - 1 />
		</cfif>
		
		<!--- get important using the old method --->
		<cfset local.oldImportant = application.g.importantGateway.getImportant_OLD(argumentCollection=arguments) />
		<cfset local.expiration_date = $.run.it('importantService.getExpirationDate') />
		
		<!--- update old important to new style --->
		<cfloop query="local.oldImportant">
			<cfset $.database.save(
				table="important",
				id=local.oldImportant.id,
				expiration_date=local.expiration_date
			) />
		</cfloop>
		
		<!--- return the updated important items --->
		<cfreturn application.g.importantGateway.getImportant(argumentCollection=arguments) />
	
	</cffunction>

	<!--- --->
	
	<cffunction name="saveSortOrder" access="public" output="true" returntype="string">
		<cfargument name="ids" required="true" />
		
		<cfloop list="#arguments.ids#" index="local.i" item="local.item">
			<cfset $.database.save(
				table="important",
				id=local.item,
				sort_order=local.i
			) />
		</cfloop>

		<cfreturn true />
	
	</cffunction>

	<!--- --->
		
	<cffunction name="getExpirationDate" access="public" output="true" returntype="string">
		
		<cfset var local = {} />

		<!--- find the current day of week --->
		<cfset local.currentTime = $.date.applyLocalization(now()) />
		<cfset local.currentDate = left(local.currentTime,10) />
		<cfset local.dayOfWeek = dayOfWeek(local.currentTime) />
		<cfset local.targetDayOfWeek = 7 />
		
		<!--- start with an add --->
		<cfset local.dayOfWeek++ />
		<cfset local.counter = 2 />
		<cfloop from="1" to="18" index="local.i">
			<!--- rollover for new week --->
			<cfif local.dayOfWeek eq 8>
				<cfset local.dayOfWeek = 1 />
			</cfif>
			<cfif local.dayOfWeek eq local.targetDayOfWeek>
				<cfbreak />
			</cfif>
			<cfset local.dayOfWeek ++ />
			<cfset local.counter ++ />
		</cfloop>
		
		<cfreturn local.expiration_date = $.date.localToUTC(dateAdd('d',local.counter,local.currentDate)) />
			
	</cffunction>

	<!--- --->

</cfcomponent>